import qt_shiyong_rc
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from mysql import mysql
import cv2
from get_face_encoding import get_encoding
from get_face_encoding import to_str
import face_recognition
from PyQt5.QtGui import *


class gengxin_xinxi(object):
    def setupUi(self, gengxin_xinxi):
        gengxin_xinxi.setObjectName("gengxin_xinxi")
        gengxin_xinxi.resize(695, 459)
        gengxin_xinxi.setMinimumSize(QtCore.QSize(695, 459))
        gengxin_xinxi.setMaximumSize(QtCore.QSize(695, 459))
        gengxin_xinxi.setStyleSheet("background-image: url(:/新前缀/好看的图片.jpg);")
        
        self.gengxin_renlian = QtWidgets.QLabel(gengxin_xinxi)

        self.label = QtWidgets.QLabel(gengxin_xinxi)
        self.label.setGeometry(QtCore.QRect(210, 10, 371, 61))
        font = QtGui.QFont()
        font.setFamily("黑体")
        font.setPointSize(18)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.gengxin_gengxin = QtWidgets.QPushButton(gengxin_xinxi)
        self.gengxin_gengxin.setGeometry(QtCore.QRect(470, 290, 211, 61))
        self.gengxin_gengxin.setStyleSheet("image: url(:/新前缀/更新人脸.png);")
        self.gengxin_gengxin.setText("")
        self.gengxin_gengxin.setObjectName("gengxin_gengxin")
        self.gengxin_shanchu = QtWidgets.QPushButton(gengxin_xinxi)
        self.gengxin_shanchu.setGeometry(QtCore.QRect(470, 370, 211, 61))
        self.gengxin_shanchu.setStyleSheet("image: url(:/新前缀/删除信息 个人状态- 2- 历史帖子.png);")
        self.gengxin_shanchu.setText("")
        self.gengxin_shanchu.setObjectName("gengxin_shanchu")
        self.gengxin_btn_xiangji = QtWidgets.QPushButton(gengxin_xinxi)
        self.gengxin_btn_xiangji.setGeometry(QtCore.QRect(470, 210, 211, 71))
        self.gengxin_btn_xiangji.setStyleSheet("image: url(:/新前缀/打开相机.png);")
        self.gengxin_btn_xiangji.setText("")
        self.gengxin_btn_xiangji.setObjectName("gengxin_btn_xiangji")
        self.layoutWidget = QtWidgets.QWidget(gengxin_xinxi)
        self.layoutWidget.setGeometry(QtCore.QRect(470, 70, 231, 131))
        self.layoutWidget.setObjectName("layoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_2 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(12)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.gengxin_xuehao = QtWidgets.QLineEdit(self.layoutWidget)
        self.gengxin_xuehao.setObjectName("gengxin_xuehao")
        self.horizontalLayout.addWidget(self.gengxin_xuehao)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_4 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(12)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_3.addWidget(self.label_4)
        self.gengxin_leixing = QtWidgets.QLineEdit(self.layoutWidget)
        self.gengxin_leixing.setMinimumSize(QtCore.QSize(0, 0))
        self.gengxin_leixing.setObjectName("gengxin_leixing")
        self.horizontalLayout_3.addWidget(self.gengxin_leixing)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_3 = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(12)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_2.addWidget(self.label_3)
        self.gengxin_xiangzi = QtWidgets.QLineEdit(self.layoutWidget)
        self.gengxin_xiangzi.setObjectName("gengxin_xiangzi")
        self.horizontalLayout_2.addWidget(self.gengxin_xiangzi)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.gengxin_zhuangtailan = QtWidgets.QTextBrowser(gengxin_xinxi)
        self.gengxin_zhuangtailan.setGeometry(QtCore.QRect(0, 430, 701, 31))
        self.gengxin_zhuangtailan.setMinimumSize(QtCore.QSize(0, 0))
        self.gengxin_zhuangtailan.setObjectName("gengxin_zhuangtailan")

        self.retranslateUi(gengxin_xinxi)
        QtCore.QMetaObject.connectSlotsByName(gengxin_xinxi)

    def retranslateUi(self, gengxin_xinxi):
        _translate = QtCore.QCoreApplication.translate
        gengxin_xinxi.setWindowTitle(_translate("gengxin_xinxi", "更新信息系统"))
        self.label.setText(_translate("gengxin_xinxi", "人脸识别信息更新系统"))
        self.label_2.setText(_translate("gengxin_xinxi", "学号："))
        self.label_4.setText(_translate("gengxin_xinxi", "类型："))
        self.label_3.setText(_translate("gengxin_xinxi", "信息："))
        self.gengxin_zhuangtailan.setHtml("信息提示")
        self.new_set()
    
    def new_set(self):
        self.gengxin_gengxin.clicked.connect(self.gengxin_xinxi)
        self.gengxin_shanchu.clicked.connect(self.shanchu_xinxi)
        self.gengxin_btn_xiangji.clicked.connect(self.dakai_xiangji)
        
        # 给按键设置提示
        self.gengxin_gengxin.setToolTip("更新信息")
        self.gengxin_shanchu.setToolTip("删除信息")
        self.gengxin_btn_xiangji.setToolTip("打开相机")
        self.gengxin_leixing.setToolTip("只能填写:班级，姓名，电话，性别，人脸")
        self.xiangji_zhuangtai = 1 # 定义一个变量，用来打开和关闭相机

    def dakai_xiangji(self):
        if self.xiangji_zhuangtai == 1:
            
            self.gengxin_renlian.setGeometry(QtCore.QRect(30, 70, 401, 361))
            self.gengxin_renlian.setObjectName("gengxin_renlian")

            self.gengxin_zhuangtailan.setHtml("相机正在打开，请稍等")
            # 打开相机
            self.capture = cv2.VideoCapture(0)  
            self.gengxin_zhuangtailan.setHtml("相机已经打开")
            self.xiangji_zhuangtai=2
            ret, self.frame = self.capture.read()
            self.frame = cv2.resize(self.frame,(440,350))
            self.frame=cv2.cvtColor(self.frame,cv2.COLOR_BGR2RGB)
            self.Qframe=QImage(self.frame.data,self.frame.shape[1],self.frame.shape[0],self.frame.shape[1]*3,QImage.Format_RGB888)
            self.gengxin_renlian.setPixmap(QPixmap.fromImage(self.Qframe))
              
        else:
            self.xiangji_zhuangtai = 1 
            self.capture.release()
            self.gengxin_renlian.setGeometry(QtCore.QRect(1000, 70, 1001, 361))
            self.gengxin_zhuangtailan.setHtml("相机已经关闭")

    def gengxin_xinxi(self):
        id = self.gengxin_xuehao.text()
        if id:
            self.gengxin_zhuangtailan.setHtml("信息正在更新，请稍等")
            leixing = self.gengxin_leixing.text()
            xinxi = self.gengxin_xiangzi.text()
            updata_face = mysql(id= id)
            if leixing == "人脸":
                if self.xiangji_zhuangtai == 2:
                    # 读取图像信息
                    face_encoding = get_encoding(self.frame)
                    face_str = to_str(face_encoding)
                    state = updata_face.updata(leixing="face", xinxi=face_str)
                else:
                    self.gengxin_zhuangtailan.setHtml("由于更新的数据是人脸，请打开相机")
                    return      

            elif leixing == "班级":
                state = updata_face.updata(leixing="class", xinxi=xinxi)

            elif leixing == "姓名":
                state = updata_face.updata(leixing="name", xinxi=xinxi)

            elif leixing == "电话":
                state = updata_face.updata(leixing="phone", xinxi=xinxi)

            elif leixing =="性别":
                state = updata_face.updata(leixing="gender", xinxi=xinxi)
            
            else:
                self.gengxin_zhuangtailan.setHtml("请检查信息是否正确，如果类型是人脸，请检查相机是否打开")
                return 

            if state ==1:
                self.gengxin_zhuangtailan.setHtml("信息更新成功")
            else:
                self.gengxin_zhuangtailan.setHtml("信息更新失败，错误代码：" + str(state))
        else:
            self.gengxin_zhuangtailan.setHtml("请输入学号后在进行信息更新")


    def shanchu_xinxi(self):
        
        id = self.gengxin_xuehao.text()
        if id:
            self.gengxin_zhuangtailan.setHtml("学生信息正在删除，请稍等")
            delete = mysql(id= id)
            state = delete.delete()
            if state == 1:
                self.gengxin_zhuangtailan.setHtml("学生信息已经删除")
            else:
                self.gengxin_zhuangtailan.setHtml("信息删除失败，错误代码：" + str(state))


